/**
 * 阿里云上传文件相关
 */
import storage from './storage.js'
import request from './request.js'
class upload {
    constructor(config) {}

    _oss = storage.fetch('ossPublic')
    _companyCode = 1001
    _serverUrl = ''
    _region = 'oss-cn-beijing'
    _percentage = 0

    async _checkOssTicket() {
        if (this._oss.expiration && this._oss.expiration > +new Date()) {
            return
        }
        // 获取oss ticket
        // let data = await request(
        //     '/fmsj/TBuildingAjaxAction.do?cmd=getOssTicket'
        // )
    }

    async doUpload(file, _callback) {
        try {
            await this._checkOssTicket()
            let myDate = new Date()
            // 文件储存路径
            let datePath =
                myDate.getFullYear() + OSS.addZero(myDate.getMonth() + 1) + '/'
            let randomNum = 0
            while (randomNum.length != 4) {
                randomNum = Math.floor(Math.random() * 10000).toString()
            }

            let filename = file.name.split('.').pop()
            let randomName =
                new Date().getTime() + '-' + randomNum + '.' + filename
            let filePath = ''

            if (
                filename === 'jpg' ||
                filename === 'png' ||
                filename === 'jpeg' ||
                filename == 'gif'
            ) {
                filePath = '/pic/'
            } else if (filename === 'svg') {
                filePath = '/svg/'
            } else {
                alert('错误的文件格式')
                return
            }
            // 文件存储路径
            let fileStoragePath =
                this._companyCode + filePath + datePath + randomName
            let client = new OSS.Wrapper({
                region: this._region,
                accessKeyId: this._oss.accessKeyId,
                accessKeySecret: this._oss.accessKeySecret,
                stsToken: this._oss.securityToken,
                bucket: this._oss.bucket
            })

            client
                .multipartUpload(fileStoragePath, file, {
                    progress: function*(percentage) {
                        // 上传进度
                        this._percentage = percentage
                    },
                    headers: {
                        // 保证下载时还是原文件名，可设置值有inline 和 attachment，其中inline是将文件内容直接显示在页面而attachment则弹出对话框让用户下载
                        'Content-Disposition': `inline;filename=${encodeURIComponent(
                            file.name
                        )}`
                    }
                })
                .then(results => {
                    let url =
                        'http://' +
                        this._oss.bucket +
                        '.' +
                        this._region +
                        '.aliyuncs.com/' +
                        results.name
                    _callback(url)
                })
                .catch(err => {
                    alert('上传文件失败，请重试！')
                })
        } catch (err) {
            alert(err)
        }
    }
}
export default upload
